/**
 * 
 */
package com.geezdata.cps.ttk.integration.ddk.impl;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.geezdata.cps.base.enums.ErrorCode;
import com.geezdata.cps.base.exceptions.MyException;
import com.geezdata.cps.base.utils.BeanUtil;
import com.geezdata.cps.base.utils.EmojiUtil;
import com.geezdata.cps.base.utils.JsonUtil;
import com.geezdata.cps.sdk3rd.pinduoduo.PinduoduoClient;
import com.geezdata.cps.sdk3rd.pinduoduo.domain.PinduoduoDDKGoodsPidGenerate;
import com.geezdata.cps.sdk3rd.pinduoduo.domain.PinduoduoDDKGoodsPromotionUrl;
import com.geezdata.cps.sdk3rd.pinduoduo.request.PinduoduoDDKGoodsDeatilRequest;
import com.geezdata.cps.sdk3rd.pinduoduo.request.PinduoduoDDKGoodsPIdRequest;
import com.geezdata.cps.sdk3rd.pinduoduo.request.PinduoduoDDKGoodsPromotionUrlRequest;
import com.geezdata.cps.sdk3rd.pinduoduo.response.PinduoduoDDKGoodsDetailResult;
import com.geezdata.cps.sdk3rd.pinduoduo.response.PinduoduoDDKGoodsPidGenerateResult;
import com.geezdata.cps.sdk3rd.pinduoduo.response.PinduoduoDDKGoodsPromotionUrlResult;
import com.geezdata.cps.ttk.common.vo.PinduoduoDDKGoodsDetailResVO;
import com.geezdata.cps.ttk.domain.domain.Sharer;
import com.geezdata.cps.ttk.integration.config.TtkIntegrationConfig;
import com.geezdata.cps.ttk.integration.ddk.DdkIntegrationService;
import com.google.common.collect.Lists;



@Service
public class DdkIntegrationServiceImpl implements DdkIntegrationService {
	@Autowired
	private PinduoduoClient pinduoduoClient;
	@Autowired
	private TtkIntegrationConfig config;

    @Override
	public List<PinduoduoDDKGoodsDetailResVO> getDDKGoodsDetail(PinduoduoDDKGoodsDeatilRequest request){
    	request.setClientId(config.getPinduoduoDdkClientId());
    	request.setClientSecret(config.getPinduoduoDdkClientSecret());
        PinduoduoDDKGoodsDetailResult  ddkResult = pinduoduoClient.getDDKGoodsDetail( request);
		return BeanUtil.convertList(ddkResult.getGoodsDetails(), PinduoduoDDKGoodsDetailResVO.class);
	}

	@Override
	public PinduoduoDDKGoodsDetailResVO getDDKGoodsDetail(String goodsId) {
		PinduoduoDDKGoodsDeatilRequest request = new PinduoduoDDKGoodsDeatilRequest();
		List<String> goodsIds = Lists.newArrayList();
		goodsIds.add(goodsId);
		String goodsIdsStr = JsonUtil.toJson(goodsIds);
		
		request.setGoodsIdList(goodsIdsStr);
		List<PinduoduoDDKGoodsDetailResVO> list = getDDKGoodsDetail(request);
		if(CollectionUtils.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}
	
	@Override
	public PinduoduoDDKGoodsPidGenerateResult getPidGenerate(PinduoduoDDKGoodsPIdRequest ddkRequest) {
		ddkRequest.setClientId(config.getPinduoduoDdkClientId());
		ddkRequest.setClientSecret(config.getPinduoduoDdkClientSecret());
		return pinduoduoClient.getPidGenerate(ddkRequest);
	}

	@Override
	public PinduoduoDDKGoodsPidGenerate getPidGenerate(Sharer sharer) {
		PinduoduoDDKGoodsPIdRequest request = new PinduoduoDDKGoodsPIdRequest();
		
		List<String> nickNameList = Lists.newArrayList();
		nickNameList.add(EmojiUtil.replaceEmoji(sharer.getNickName()));
		
		String nickNameStr = JsonUtil.toJson(nickNameList);
		
		request.setNumber(1);
		request.setpIdNameList(nickNameStr);
		PinduoduoDDKGoodsPidGenerateResult result = getPidGenerate(request);
		if(CollectionUtils.isEmpty(result.getpIdList())) {
			throw new MyException(ErrorCode.INTERNAL_SERVER_ERROR);
		}
		return result.getpIdList().get(0);
	}
	
	@Override
	public PinduoduoDDKGoodsPromotionUrl getPromotionUrl(String pId, String goodsId, String customParameters, Boolean multiGroup) {
		PinduoduoDDKGoodsPromotionUrlRequest request = new PinduoduoDDKGoodsPromotionUrlRequest();
		List<String> goodsIds = Lists.newArrayList();
		goodsIds.add(goodsId);
		String goodsIdsStr = JsonUtil.toJson(goodsIds);
		
		request.setClientId(config.getPinduoduoDdkClientId());
		request.setClientSecret(config.getPinduoduoDdkClientSecret());
		
		request.setpId(pId);
		request.setGoodsIdList(goodsIdsStr);
		request.setGenerateShortUrl(true);
		request.setGenerateWeApp(true);
		request.setMultiGroup(multiGroup);
		request.setCustomParameters(customParameters);
		PinduoduoDDKGoodsPromotionUrlResult ddkResult = pinduoduoClient.getPromotionUrl( request);
		if(ddkResult == null || CollectionUtils.isEmpty(ddkResult.getGoodsPromotionUrlList())) {
			throw new MyException(ErrorCode.INTERNAL_SERVER_ERROR);
		}
		return ddkResult.getGoodsPromotionUrlList().get(0);
	}
	
	@Override
	public List<PinduoduoDDKGoodsDetailResVO> getDDKGoodsDetails(String goodsId){
        PinduoduoDDKGoodsDeatilRequest ddkRequest = convent2PinduoduoDDKGoodsDetailRequest(goodsId);
        PinduoduoDDKGoodsDetailResult ddkResult = pinduoduoClient.getDDKGoodsDetail( ddkRequest);
        return BeanUtil.convertList(ddkResult.getGoodsDetails(), PinduoduoDDKGoodsDetailResVO.class);
	}

	private PinduoduoDDKGoodsDeatilRequest convent2PinduoduoDDKGoodsDetailRequest(String goodsId) {
        PinduoduoDDKGoodsDeatilRequest ddkRequest = new PinduoduoDDKGoodsDeatilRequest();
		ddkRequest.setClientId(config.getPinduoduoDdkClientId());
		ddkRequest.setClientSecret(config.getPinduoduoDdkClientSecret());
		String goodsIdList = "[" + goodsId + "]";
		ddkRequest.setGoodsIdList(goodsIdList );
		return ddkRequest;
	}
}
